﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DbSharp.Utils;
using System.IO;
using DbSharp.Utils.IO;

namespace DbSharp.Tools.Xml2Excel
{
    partial class FormXmlToExcel : Form
    {
        readonly string m_Command_Console_App = "Xml2Excel.Console";

        public FormXmlToExcel()
        {
            InitializeComponent();
        }

        private void buttonConvert_Click(object sender, EventArgs e)
        {
            string xmlFile;
            xmlFile = textBoxXmlFile.Text.Trim();
            string excelFile;
            excelFile = textBoxExcelFile.Text.Trim();
            if (!File.Exists(xmlFile))
            {
                string msgFmt = "Source file '{0}' does not exist.";
                string msg = string.Format(msgFmt, xmlFile);

                MessageBox.Show(msg, "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(excelFile))
            {
                string msg = "Please input one file name as export destination.";

                MessageBox.Show(msg, "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            string extryApp = "\"" + PathHelper.GetAppPath() + m_Command_Console_App + "\"";

            string commandLineFmt = " {0} {1}";
            string commandLine = string.Format(commandLineFmt, "\"" + xmlFile + "\"", "\"" + excelFile + "\"");
            int exitCode;
            string outputInfo;
            ProcessLauncher.RunConsoleApp(extryApp, commandLine, out exitCode, out outputInfo);
            if (string.IsNullOrEmpty(outputInfo))
                outputInfo = "Conversion completed.";
            richTextBoxOutputInfo.AppendText(outputInfo);
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonBrowse1_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.Title = "Select one xml file as source";
            openFileDialog1.FileName = textBoxXmlFile.Text;
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBoxXmlFile.Text = openFileDialog1.FileName;
            }
        }

        private void buttonBrowse2_Click(object sender, EventArgs e)
        {
            openFileDialog1.CheckFileExists = false;
            openFileDialog1.Title = "Select one excel file as destination";
            openFileDialog1.FileName = textBoxExcelFile.Text;
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBoxExcelFile.Text = openFileDialog1.FileName;
            }
        }

      
    }
}
